/**
 * Copyright 2011 The ForPlay Authors
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package core;

import java.util.ArrayList;
import java.util.List;

import static forplay.core.ForPlay.*;

import entities.Kastila;
import entities.Katipunero;
import entities.Bullet;
import forplay.core.Game;
import forplay.core.GroupLayer;
import forplay.core.Image;
import forplay.core.ImageLayer;
import forplay.core.Pointer;
import forplay.core.Keyboard;

public class JRizal implements Game, Pointer.Listener,  Keyboard.Listener{
	GroupLayer battleGroundLayer;
	List<Kastila> kastilas = new ArrayList<Kastila>(0);
	List<Bullet> bullets = new ArrayList<Bullet>(0);

  Katipunero katipunero;
	@Override
	public void init() {
	  
	  //set screen size
	  graphics().setSize(360, 620);
	  
		// create and add background image layer
		Image bgImage = assetManager().getImage("images/bg.png");
		ImageLayer bgLayer = graphics().createImageLayer(bgImage);
		graphics().rootLayer().add(bgLayer);

		// create a group layer to hold the peas
		battleGroundLayer = graphics().createGroupLayer();
		graphics().rootLayer().add(battleGroundLayer);

		// preload images into the asset manager cache
		assetManager().getImage(Kastila.IMAGE);
		assetManager().getImage(Katipunero.IMAGE);

		
		// add a listener for pointer (mouse, touch) input
		pointer().setListener(this);
		
		// add keyboard listener
		keyboard().setListener(this);
		
    //place katipunero
    katipunero = new Katipunero(battleGroundLayer, 180, 560);		
	}

	@Override
	public void onPointerDrag(float x, float y) {
	}

	@Override
	public void onPointerEnd(float x, float y) {

	}
	
	@Override
	public void onPointerStart(float x, float y) {
	}

	 @Override
   public void onKeyDown(int keyCode){

	 }
	 
	 @Override
	 public void onKeyUp(int keyCode){
	   
	   Bullet bullet =  new Bullet(battleGroundLayer, 180, 560);
	   bullets.add(bullet);
	 }
	 
	
	@Override
	public void paint(float alpha) {
		// layers automatically paint themselves (and their children). The
		// rootlayer
		// will paint itself, the background, and the pea group layer
		// automatically
		// so no need to do anything here!
	}

	@Override
	public void update(float delta) {

    katipunero.update(delta);
    
  /*  if ((delta % 30) == 0)
    {
      //enemy placements
      double ran = Math.random();
      Kastila kastila = new Kastila(battleGroundLayer, (float)(ran*100)%360, 0);
      kastilas.add(kastila);
    }
*/
    if(Math.floor(Math.random() * 20) == 1)
    {
      Kastila kastila = new Kastila(battleGroundLayer, (float)(Math.random()*360) , 0);
      kastilas.add(kastila);
       
    }

		for (Kastila k : kastilas) {
		  k.update(delta,katipunero.x, katipunero.y);
		}
		
		for (Bullet b : bullets){
		  b.update(delta);
		}

	}

	@Override
	public int updateRate() {
		return 60;
	}
}
